"use strict";
cc._RF.push(module, '61df2wUSU1KwrEBASSq7XOu', 'CardLib');
// script/Game/Libs/CardLib.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var GridsLib_1 = require("./GridsLib");
var ResLoader_1 = require("../../Core/Libs/ResLoader");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var CardLib = /** @class */ (function (_super) {
    __extends(CardLib, _super);
    function CardLib() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cardBack = null;
        _this.cardFront = null;
        _this.attack = null;
        _this.recover = null;
        _this.defense = null;
        _this.skill = null;
        _this.normal = null;
        _this.container = null;
        //卡牌数值
        _this._hp = 0;
        _this._maxHp = 0;
        //额外的护盾
        _this._defenseValue = 0;
        return _this;
        // update (dt) {}
    }
    CardLib.prototype.init = function (w, h) {
        this.node.width = w;
        this.node.height = h;
        this.cardFront.width = w;
        this.cardFront.height = h;
        this.cardBack.width = w;
        this.cardBack.height = h;
        this.reset();
    };
    Object.defineProperty(CardLib.prototype, "hp", {
        get: function () {
            if (this.cardInfo.type != GridsLib_1.CardType.Hero) {
                return 0;
            }
            return this._hp + this._defenseValue;
        },
        enumerable: false,
        configurable: true
    });
    CardLib.prototype.addHp = function (n) {
        if (this.cardInfo.type != GridsLib_1.CardType.Hero) {
            return;
        }
        //扣血
        if (n < 0) {
            var subHp = Math.abs(n);
            //先减去护盾值，如果能抵消 那么直接返回
            if (this._defenseValue > 0) {
                //护盾能抵扣
                if (this._defenseValue >= subHp) {
                    this.addDefenseValue(-subHp);
                    return;
                }
                //不能全部抵扣 计算差值 再扣血
                n = this._defenseValue - subHp;
                this.addDefenseValue(-subHp);
            }
        }
        this._hp += n;
        this._hp = this._hp >= this._maxHp ? this._maxHp : this._hp;
        this.attack.getChildByName("value").getComponent(cc.Label).string = this._hp + "/" + this._maxHp;
        this.attack.active = true;
    };
    CardLib.prototype.addMaxHp = function (n) {
        if (!n) {
            return;
        }
        if (this.cardInfo.type != GridsLib_1.CardType.Hero && this.cardInfo.type != GridsLib_1.CardType.Boss) {
            return;
        }
        this._maxHp = n;
        this.attack.getChildByName("value").getComponent(cc.Label).string = this._hp + "/" + this._maxHp;
        this.attack.active = true;
    };
    //非英雄卡设置值
    CardLib.prototype.addValue = function (n) {
        var parentNode;
        switch (this.cardInfo.type) {
            case GridsLib_1.CardType.Hero:
            case GridsLib_1.CardType.Trap:
            case GridsLib_1.CardType.Boss:
            case GridsLib_1.CardType.Monster:
                parentNode = this.attack;
                break;
            case GridsLib_1.CardType.Health:
                parentNode = this.recover;
                break;
            case GridsLib_1.CardType.Shield:
                parentNode = this.defense;
                break;
            case GridsLib_1.CardType.Treasure:
            case GridsLib_1.CardType.Coin:
                parentNode = this.normal;
                break;
            case GridsLib_1.CardType.Skill:
                parentNode = this.skill;
        }
        parentNode.getChildByName("value").getComponent(cc.Label).string = (this.cardInfo.value + n).toString();
    };
    Object.defineProperty(CardLib.prototype, "defenseValue", {
        get: function () {
            return this._defenseValue;
        },
        enumerable: false,
        configurable: true
    });
    CardLib.prototype.addDefenseValue = function (n) {
        if (this.cardInfo.type != GridsLib_1.CardType.Hero) {
            return;
        }
        this._defenseValue += n;
        if (this._defenseValue > 0) {
            this.defense.getChildByName("value").getComponent(cc.Label).string = this._defenseValue.toString();
            this.defense.active = true;
        }
        else {
            this._defenseValue = 0;
            this.defense.active = false;
        }
    };
    CardLib.prototype.setCardAttr = function (cardInfo) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.cardInfo) {
                            this.reset();
                        }
                        this.cardInfo = cardInfo;
                        //设置背景
                        return [4 /*yield*/, this.setCardBackground()];
                    case 1:
                        //设置背景
                        _a.sent();
                        //设置角色
                        return [4 /*yield*/, this.setCardRole()];
                    case 2:
                        //设置角色
                        _a.sent();
                        //设置
                        return [4 /*yield*/, this.setCardLabel()];
                    case 3:
                        //设置
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    CardLib.prototype.reset = function () {
        this.attack.active = false;
        this.recover.active = false;
        this.defense.active = false;
        this.skill.active = false;
        this.normal.active = false;
        this.cardBack.active = true;
        this.cardFront.active = false;
        this.cardFront.getComponent(cc.Sprite).spriteFrame = null;
        //卡牌数值
        this._hp = 0;
        this._maxHp = 0;
        //额外的护盾
        this._defenseValue = 0;
    };
    CardLib.prototype.setCardBackground = function () {
        return __awaiter(this, void 0, void 0, function () {
            var assetPath, _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        assetPath = "texture/";
                        switch (this.cardInfo.type) {
                            case GridsLib_1.CardType.Hero:
                                assetPath += "card-bg-2";
                                break;
                            case GridsLib_1.CardType.Trap:
                            case GridsLib_1.CardType.Boss:
                            case GridsLib_1.CardType.Monster:
                                assetPath += "card-bg-4";
                                break;
                            default:
                                assetPath += "card-bg-3";
                        }
                        _a = this.cardFront.getComponent(cc.Sprite);
                        return [4 /*yield*/, ResLoader_1.default.load(assetPath, cc.SpriteFrame)];
                    case 1:
                        _a.spriteFrame = _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    CardLib.prototype.setCardRole = function () {
        return __awaiter(this, void 0, void 0, function () {
            var assetPrefix, _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        assetPrefix = cc.Enum(GridsLib_1.CardType)[this.cardInfo.type].toLowerCase();
                        _a = this.container.getChildByName("role").getComponent(cc.Sprite);
                        return [4 /*yield*/, ResLoader_1.default.load("texture/" + assetPrefix + "-" + this.cardInfo.id, cc.SpriteFrame)];
                    case 1:
                        _a.spriteFrame =
                            _b.sent();
                        this.container.active = true;
                        return [2 /*return*/];
                }
            });
        });
    };
    CardLib.prototype.setCardLabel = function () {
        return __awaiter(this, void 0, void 0, function () {
            var parentNode;
            return __generator(this, function (_a) {
                //英雄卡才有 最大值
                if (this.cardInfo.maxValue) {
                    this._maxHp = this.cardInfo.maxValue;
                    this.addHp(this.cardInfo.value);
                    return [2 /*return*/];
                }
                switch (this.cardInfo.type) {
                    case GridsLib_1.CardType.Hero:
                    case GridsLib_1.CardType.Trap:
                    case GridsLib_1.CardType.Boss:
                    case GridsLib_1.CardType.Monster:
                        parentNode = this.attack;
                        break;
                    case GridsLib_1.CardType.Health:
                        parentNode = this.recover;
                        break;
                    case GridsLib_1.CardType.Shield:
                        parentNode = this.defense;
                        break;
                    case GridsLib_1.CardType.Treasure:
                    case GridsLib_1.CardType.Coin:
                        parentNode = this.normal;
                        break;
                    case GridsLib_1.CardType.Skill:
                        parentNode = this.skill;
                }
                parentNode.getChildByName("value").getComponent(cc.Label).string = this.cardInfo.value.toString();
                parentNode.active = true;
                return [2 /*return*/];
            });
        });
    };
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "cardBack", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "cardFront", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "attack", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "recover", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "defense", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "skill", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "normal", void 0);
    __decorate([
        property(cc.Node)
    ], CardLib.prototype, "container", void 0);
    CardLib = __decorate([
        ccclass
    ], CardLib);
    return CardLib;
}(cc.Component));
exports.default = CardLib;

cc._RF.pop();